package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.EmployeeMapper;
import com.zretc.dao.EmployeeSalaryMapper;
import com.zretc.entity.Employee;
import com.zretc.entity.EmployeeSalary;
import com.zretc.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeSalaryMapper employeeSalaryMapper;


    @Override
    public int add(Employee employee) {

        int add = employeeMapper.add(employee);
        if (add>0){
            EmployeeSalary employeeSalary=new EmployeeSalary(1,employee.getId(),1);
            employeeSalaryMapper.add(employeeSalary);
        }
        return add;
    }

    @Override
    public int delete(Integer id) {
        int delete = employeeMapper.delete(id);
        if (delete>0){
            employeeSalaryMapper.delete(id);
        }
        return delete;
    }

    @Override
    public int update(Employee employee) {
        return employeeMapper.update(employee);
    }

    @Override
    public PageInfo<Employee> findBy(Map<String, Object> map) {
        Integer number= (Integer) map.get("number");
        Integer size= (Integer) map.get("size");
        String name = (String) map.get("name");


        if (name=="" || name ==null){
            map.put("name",null);
        }else {
            map.put("name","%"+name+"%");
        }
        PageHelper.startPage(number,size);
        List<Employee> list = employeeMapper.findBy(map);
        PageInfo<Employee> pageInfo = PageInfo.of(list);
        return pageInfo;
    }

    @Override
    public String getMaxWorkId() {
        Integer maxWorkId = employeeMapper.getMaxWorkId();
        String format = String.format("%08d", maxWorkId + 1);
        return format;
    }
}
